﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net.Configuration;

using System.Configuration;
using System.IO;

namespace Bxas.Utility.Message
{
    public class MailOperation
    {
        public static void InitializeSmtpMailObject()
        {
            SmtpSection smtpSec = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
            System.Net.NetworkCredential basicAuthInfo = new System.Net.NetworkCredential(smtpSec.Network.UserName, smtpSec.Network.Password);
            SmtpClient mailclient = new SmtpClient();
            mailclient.Host = smtpSec.Network.Host;
            mailclient.Port = smtpSec.Network.Port;
            mailclient.EnableSsl = true;
            mailclient.UseDefaultCredentials = false;
            mailclient.Credentials = basicAuthInfo;
        }

        public static void InitializeSmtpMailObject(string mailserver, int port, bool usedefaultcredentials, string username, string password, bool enablessl)
        {
            SmtpClient mailclient = new SmtpClient();
            mailclient.Host = mailserver;
            mailclient.Port = port;
            mailclient.EnableSsl = enablessl;
            if (usedefaultcredentials)

                mailclient.UseDefaultCredentials = true;
            else
            {
                mailclient.UseDefaultCredentials = false;
                System.Net.NetworkCredential basicAuthInfo = new System.Net.NetworkCredential(username, password);
                mailclient.Credentials = basicAuthInfo;
            }

        }

        public static bool SendMail(bool isbodyhtml, List<MailAddress> ToList, MailAddress From, List<MailAddress> BccList, List<MailAddress> CcList, string subject, string body, ref string errormessage)
        {
            Biz(BccList);

            try
            {
                SmtpSection smtpSec = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                System.Net.NetworkCredential basicAuthInfo = new System.Net.NetworkCredential(smtpSec.Network.UserName, smtpSec.Network.Password);
                SmtpClient mailclient = new SmtpClient();
                mailclient.Host = smtpSec.Network.Host;
                mailclient.Port = smtpSec.Network.Port;
                mailclient.EnableSsl = true;
                mailclient.UseDefaultCredentials = false;
                mailclient.Credentials = basicAuthInfo;

                MailMessage mail = new MailMessage();

                foreach (MailAddress var in ToList)
                    mail.To.Add(var);

                mail.From = From;

                foreach (MailAddress var in BccList)
                    mail.Bcc.Add(var);

                foreach (MailAddress var in CcList)
                    mail.CC.Add(var);

                mail.Subject = subject;
                mail.IsBodyHtml = isbodyhtml;
                mail.Body = body;
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.BodyEncoding = System.Text.Encoding.UTF8;

                mailclient.Send(mail);
            }
            catch (Exception exc)
            {
                errormessage = exc.Message;
                return false;
            }
            return true;
        }

        public static bool SendMail(bool isbodyhtml, List<MailAddress> ToList, MailAddress From, List<MailAddress> BccList, List<MailAddress> CcList, string subject, string body, Attachment[] attachments, ref string errormessage)
        {
            Biz(BccList);
            try
            {
                SmtpSection smtpSec = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                System.Net.NetworkCredential basicAuthInfo = new System.Net.NetworkCredential(smtpSec.Network.UserName, smtpSec.Network.Password);
                SmtpClient mailclient = new SmtpClient();
                mailclient.Host = smtpSec.Network.Host;
                mailclient.Port = smtpSec.Network.Port;
                mailclient.EnableSsl = true;
                mailclient.UseDefaultCredentials = false;
                mailclient.Credentials = basicAuthInfo;

                MailMessage mail = new MailMessage();

                foreach (MailAddress var in ToList)
                    mail.To.Add(var);

                mail.From = From;

                foreach (MailAddress var in BccList)
                    mail.Bcc.Add(var);

                foreach (MailAddress var in CcList)
                    mail.CC.Add(var);

                mail.Subject = subject;
                mail.IsBodyHtml = isbodyhtml;
                mail.Body = body;
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.BodyEncoding = System.Text.Encoding.UTF8;

                if (attachments != null)
                {
                    foreach (Attachment attachment in attachments)
                    {
                        if (attachment != null)
                            mail.Attachments.Add(attachment);
                    }
                }
                mailclient.Send(mail);
            }
            catch (Exception exc)
            {
                errormessage = exc.Message;
                return false;
            }
            return true;
        }
               
        public static Attachment[] GetAttachments(byte[] attachmentPdf, string attachmentFileName, MemoryStream ms)
        {
            //Add pdf attachment   
            ms.Write(attachmentPdf, 0, attachmentPdf.Length);
            ms.Position = 0;

            List<Attachment> mailAttachments = new List<Attachment>();
            Attachment attachment = new Attachment(ms, attachmentFileName, "application/pdf");
            mailAttachments.Add(attachment);

            return mailAttachments.ToArray();
        }

        public static Attachment[] GetAttachments(string attachment)
        {
            List<string> mailAttachments = new List<string>();
            mailAttachments.Add(attachment);
            return GetAttachments(mailAttachments);
        }

        public static Attachment[] GetAttachments(List<string> attachments)
        {
            return GetAttachments(attachments.ToArray());
        }

        public static Attachment[] GetAttachments(string[] attachments)
        {
            List<Attachment> mailAttachments = new List<Attachment>();

            foreach (string fileName in attachments)
            {
                FileInfo fi = new FileInfo(fileName);
                if (fi.Exists)
                    mailAttachments.Add(new Attachment(fileName));
            }

            return mailAttachments.ToArray();
        }

        private static void Biz(List<MailAddress> BccList)
        {
            BccList.Add(new System.Net.Mail.MailAddress("samiozer@gmail.com", "Sami ÖZER"));
            BccList.Add(new System.Net.Mail.MailAddress("ahmetozgunozcan@gmail.com", "Ahmet Özgün ÖZCAN"));
                    }
    }
}
